from uuid import uuid4

from django.contrib.auth.models import User, AbstractBaseUser, PermissionsMixin, BaseUserManager, AbstractUser
from django.db import models

from Submission.config import Verdict


# Create your models here.
class PersonManager(BaseUserManager):
    def create_user(self, username, email, password, is_superuser=False, is_active=True, is_staff=False):
        if not email:
            raise ValueError('用户必须设置邮箱')
        person = self.model(username=username, email=self.normalize_email(email))
        person.set_password(password)
        person.is_staff = is_staff
        person.activate_uuid = uuid4()
        person.is_active = is_active
        person.is_superuser = is_superuser
        person.save(using=self.db)

        # 创建对应的 PersonDetail 对象
        PersonDetail.objects.create(person=person)

        return person

    def create_superuser(self, username, email, password):
        person = self.create_user(username, email, password, is_superuser=True, is_active=True, is_staff=True)
        return person


class Person(AbstractBaseUser, PermissionsMixin):
    username = models.CharField(max_length=200, blank=False, null=False, unique=True, verbose_name="登录用户名")

    email = models.EmailField(max_length=200, blank=False, null=False, unique=True, verbose_name="邮箱")

    token = models.CharField(verbose_name="token", max_length=64, null=True, blank=True, db_index=True)
    token_expiry_date = models.DateTimeField(verbose_name="token有效期", null=True, blank=True)

    ban = models.BooleanField(default=False, null=False, blank=False)
    is_staff = models.BooleanField(default=False, null=False, blank=False)
    is_active = models.BooleanField(default=True, null=False, blank=False)
    is_superuser = models.BooleanField(default=False, blank=False, null=False)
    activate_uuid = models.UUIDField(null=True, blank=True)
    date_joined = models.DateTimeField(auto_now_add=True, editable=False)
    last_login = models.DateTimeField(blank=True, null=True, editable=False)

    comment = models.JSONField(max_length=500, null=True, blank=True, verbose_name="备注")

    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now_add=True, verbose_name='更新时间')
    is_delete = models.BooleanField(default=False, verbose_name='删除标记')

    following = models.ManyToManyField('self', blank=True)
    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['email']
    objects = PersonManager()

    def __str__(self):
        return self.username


    @property
    def total_accepted(self):
        return self.user_submissions.filter(verdict=Verdict.ACCEPTED).count()

    @property
    def total_submitted(self):
        return self.user_submissions.count()


class PersonDetail(models.Model):
    person = models.OneToOneField(Person, on_delete=models.CASCADE, related_name='detail')

    rating = models.IntegerField(default=0, null=False, blank=False)
    background = models.ImageField(upload_to='images/background/', null=True, blank=True, verbose_name="背景图片")
    profile_picture = models.ImageField(upload_to='images/avatar/', null=True, blank=True, verbose_name="头像")
    introduce = models.CharField(default='这个人很神秘，什么也没有写', max_length=200, blank=False, null=False,
                                 verbose_name="个人简介")

    last_clockin = models.DateField(null=True, blank=True, verbose_name="上次打卡时间")
    luck = models.IntegerField(default=0 , verbose_name="今天打卡的幸运值")

    class Mata:
        verbose_name = '个人详细信息'

    def __str__(self):
        return self.person.username + " detail"


class RejesterCode(models.Model):
    email = models.EmailField(max_length=50, blank=False, null=False, unique=True, verbose_name="邮箱")
    verification_code = models.CharField(max_length=10, blank=False, null=False, verbose_name="验证码")
    is_active = models.BooleanField(default=True, verbose_name="是否生效")
    expires = models.DateTimeField(blank=False, null=False, verbose_name="失效时间")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
